package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 映像网新闻
 * @author Persh
 *
 */
public class HnrNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://[a-z]*.hnr.cn/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h2.atc_tit").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.bt").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("p.hgt_28").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("h1.atc_tit").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("div.bg_f0f0f0").text();
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.article_txt"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.con"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.ml25"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
				}
			}
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("span.mr15").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("p.txtcenter").text();
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");
		date=DateUtils.matchDate(doc.select("span.mr15").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.sx").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("p.hgt_22").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("p.f12_666").text());
				}
				if(date == null){
					date=DateUtils.matchDate(url);
				}
			}
		}


		if(StringUtils.isBlank(title) || date == null){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		HnrNewsAnalyse analyse=new HnrNewsAnalyse();
		String url="http://m.hnr.cn/3gcar/201403/t20140328_899837.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
